<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="服务图标" prop="pic">
        <imgUpload :limit="1" @input="upload" :value="form.pic" />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select v-model="form.serviceType" placeholder="请选择服务类型">
          <el-option v-for="(item, index) in types" :key="index" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务说明" prop="serviceIllustrate">
        <el-input type="textarea" maxlength="100" show-word-limit v-model="form.serviceIllustrate"
          placeholder="请输入服务说明" />
      </el-form-item>

      <el-form-item label="服务介绍" prop="description">
        <editor v-model="form.description" height="500px" />
      </el-form-item>
      <el-form-item label="启用状态" prop="enabled">
        <el-switch v-model="form.enabled" :inactive-value="0" :active-value="1"></el-switch>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { STATUS } from '../../constant/constant'

import {
  listManager,
  listType,
  getManager,
  delManager,
  addManager,
  updateManager
} from '../../api/service-manager/manager'
import router from '@/router'
export default {
  data() {
    return {
      types: [],
      status: STATUS,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pic: [{ required: true, message: '图标不能为空', trigger: 'blur' }],
        serviceIllustrate: [
          { required: true, message: '服务说明不能为空', trigger: 'blur' }
        ],
        serviceType: [
          { required: true, message: '服务类型id不能为空', trigger: 'change' }
        ],
        enabled: [
          {
            required: true,
            message: '0, 不启用; 1,启用不能为空',
            trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '服务介绍不能为空', trigger: 'change' },
          // 自定义规则
          { validator: this.checkDescription, trigger: ['blur', 'change'] }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  components: {
    imgUpload: () => import('../../components/ImageUpload/oss.vue')
  },

  mounted() {
    listType(this)
  },

  created() {
    getManager(this.$route.params.id).then((response) => {
      this.form = response.data
      this.open = true
      this.title = '修改服务管理'
    })
  },

  methods: {
    upload(param) {
      this.form.pic = param
      console.log('param', param)
    },

    checkDescription(rule, value, callback) {
      const cleanedContent = value.replace(/<[^>]*>/g, '').trim();

      if (cleanedContent === '') {
        callback(new Error('服务介绍不能为空'))
        console.log('Editor is empty');
      } else {
        callback()
      }
    },

    /** 提交按钮 */
    submitForm() {

      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        updateManager(this.form).then((response) => {
          this.$modal.msgSuccess('操作成功')

          this.$router.push({
            path: '/manager/index'
          })
        })
      })
    },

    // 取消按钮
    cancel() {
      this.reset();
      this.$router.back()
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        pic: null,
        serviceIllustrate: null,
        serviceType: null,
        typeName: null,
        enabled: null,
        description: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    }
  }
}
</script>


<style>
textarea {
  resize: none !important;
}
</style>
